﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Web.Common;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;
using System.Text;

namespace FortuneHeaven.Web.Pages.UserCenter
{
    public partial class UserInformation : CheckAuthPage
    {
        public string Province;
        public string City;
        public string Year;
        public string Month;
        public string Day;
        public string FaceID;
        public string Gender;
        protected void Page_Load(object sender, EventArgs e)
        {
            //Province = Request["ddlProvince"];
            //City = Request["ddlCity"];
            Year = Request["myyear"];
            Month = Request["mymonth"];
            Day = Request["myday"];
            FaceID = Request["in_FaceID"];
            Gender = Request["Gender"];
            if (!IsPostBack)
            {
                SetPageHeader(FortuneHeavenCommon.Enum.MainPageTabType.hidden);
                UserInforBO userBO = new UserInforBO();
                AccountsInfoBO acountBO = new AccountsInfoBO();

                UserInfor infor = userBO.GetUserInfor(AuthorityCenter.CurrentUserID);
                AccountsInfo acount = acountBO.GetAcount(AuthorityCenter.CurrentUserID);
                if (acount == null)
                {
                    return;
                }
                if (infor == null)
                {
                    infor = new UserInfor();
                }
                ltName.Text = AuthorityCenter.CurrentUserName;
                //Province = infor.Province;
                //City = infor.City;

                //if (string.IsNullOrEmpty(Province))
                //{
                //    Province = "上海市";
                //    City = "黄浦区";
                //}

                if (infor.Birthday != DateTime.MinValue)
                {
                    Year = infor.Birthday.Year.ToString();
                    Month = infor.Birthday.Month.ToString();
                    Day = infor.Birthday.Day.ToString();
                }
                else
                {
                    Year = "1985";
                    Month = "1";
                    Day = "1";
                }
                FaceID = acount.FaceID.ToString();

                spIDCard.InnerText = GetSecu(infor.IDCard);
                spName.InnerText = acount.Accounts;

                txtMail.Text = infor.RegisterEmail;
                txtMSN.Text = infor.MSN;
                txtNickName.Text = infor.NickName;
                txtQQ.Text = infor.QQ;
                txtRealName.Text = acount.Add_RealName;
                txtMobile.Text = acount.Add_MobilePhone;
            }
        }

        public string GetSecu(string input)
        {
            StringBuilder builder = new StringBuilder();
            if (!string.IsNullOrEmpty(input) && input.Length > 2)
            {
                for (int index = 0; index < input.Length - 2; index++)
                {
                    builder.Append("*");
                }
                builder.Append(input.Substring(input.Length - 2, 2));
            }
            return builder.ToString();
        }

        public void SaveInfor(object sender, EventArgs e)
        {
            try
            {
                DateTime birth = DateTime.Now;
                if (!string.IsNullOrEmpty(Year) && !string.IsNullOrEmpty(Month) && !string.IsNullOrEmpty(Day))
                {
                    birth = DateTime.Parse(string.Format("{0}/{1}/{2}", Year, Month, Day));
                }
                UserInforBO userBO = new UserInforBO();
                AccountsInfoBO acountBO = new AccountsInfoBO();

                UserInfor infor = userBO.GetUserInfor(AuthorityCenter.CurrentUserID);
                if (infor == null)
                {
                    infor = new UserInfor();
                }
                AccountsInfo acount = acountBO.GetAcount(AuthorityCenter.CurrentUserID);

                //infor.Province = Province;
                //infor.City = City;
                infor.Birthday = birth;
                infor.RegisterEmail = txtMail.Text;
                infor.MSN = txtMSN.Text;
                infor.NickName = txtNickName.Text;
                infor.QQ = txtQQ.Text;
               
                userBO.SaveUserInfor(infor);

                acount.Add_MobilePhone = txtMobile.Text;
                acount.FaceID = GetInt(FaceID);
                acount.Add_RealName = txtRealName.Text;
                acountBO.Save(acount);
                ClientScript.RegisterStartupScript(this.GetType(), "Success", "alert('保存成功')", true);
            }
            catch
            {
                Response.Write("<script>alert('服务器发生错误。请稍后再试。')</script>");
            }
        }
    }
  
}